
local unit_attribute = require 'types.unit_attribute'
local attribute = unit_attribute.field
-- for name in pairs(unit_attribute.field) do
--     table.insert(attribute,name)
-- end

function LoadItemSX(unit,data,add)
    for name, value in sortpairs(data) do 
        if attribute[name] then 
            unit:add(name, add*value)
        end
    end 
end

-- 添加物品属性
ac.game:on '物品-被拾取' (function(_, item, unit)
    
    if unit:dispatch("单位-检查物品合成", unit, item) then 
        unit:notify('单位-合成物品', unit, item)
        return
    end
    
    -- 消耗品和材料自动叠加
    if item:can_merge() then 
        local items = unit:get_items()
        local merge 
        for i, v in ipairs(items) do 
            if v ~= item and v:get_id() == item:get_id() then 
                merge = v 
                break 
            end 
        end 
        if merge then 
            merge:add_count(item:get_count())
            item:remove()
            return 
        end
    end 

    item.is_picked = true
    if unit:has_restriction "闭锁" then 
        return 
    end 
    unit:notify('单位-添加物品属性', unit, item)
end)

-- 如果物品绑定英雄
-- 删除物品属性
-- 删除物品模型附加
ac.game:on '物品-被丢弃' (function(_, item, unit)
    if not item.is_picked then 
        return false 
    end 
    item.is_picked = false
    if unit:has_restriction "闭锁" then 
        return 
    end 
    unit:notify('单位-移除物品属性', unit, item)
end)

ac.game:on '单位-添加物品属性' (function (_, unit, item)
    if item then 
        for name, value in sortpairs(item) do 
            if attribute[name] then 
                unit:add(name, value)
            end
        end 
    end 
    if item['技能'] then
        item.abi = unit:add_skill(item['技能'],'隐藏')
    end
    --绑定附加特效
    if item.profile then 
        item.profile_list = {}
        for socket, models in item.profile:gmatch('<(.-)>:{(.-)}') do 
            if socket and models then 
                local info = ac.split(models,';')
                for _, model in ipairs(info) do 
                    local amodel = string.trim(model)
                    if amodel:find('[%.][Mm][Dd][XxLl]') then 
                        local effect = unit:add_attach(amodel, socket)
                        table.insert(item.profile_list, effect)
                    end
                end

            end
        end
    end
    unit:notify('单位-获得物品效果',unit,item)
    item:notify('on_get', unit)
end)

ac.game:on '单位-移除物品属性' (function (_, unit, item)
    for name, value in sortpairs(item) do 
        if attribute[name] then 
            unit:add(name, -value)
        end
    end 
    if item.abi then
        item.abi:remove()
    end
    if item.profile_list then 
        for _, effect in ipairs(item.profile_list) do 
            effect:remove()
        end
    end
    item.profile_list = nil
    unit:notify('单位-失去物品效果',unit,item)
    item:notify('on_lose', unit)  
    item:gc_clear()
end)

ac.game:on '单位-拾取规则检查' (function (_, unit, item)

    -- 失败描述
    local tips = ac.lni("tips", "pickup-fail")

    -- 玩家
    local player = unit:get_owner()

    -- 先让物品自己判断能不能捡
    local pickup, reason = item:notify("on_pickup", unit)

    if pickup == true or pickup == nil then 

        -- 这里让他直接是true
        pickup = true

        if item.pickup_flag > 0 then 
    
            local items = unit:get_items()
            local name = item:get_id()

            for _, compare in ipairs(items) do 
                if compare ~= item then 
                    if name == compare:get_id() and item.pickup_flag >= 2 then 
                        pickup = false
                        reason = item.pickup_message or (tips) and tips["唯一"]
                        break
                    elseif item.pickup_flag >= 1 and compare.race and compare.race == item.race then 
                        pickup = false
                        reason = (tips) and tips[item.race]
                        break
                    end
                end 
            end 
        end 
    end 

    if pickup == true and not item:is_class("powerup") and unit:get_inventory_stock() < 1 then
        pickup = false
        reason = '物品栏已满!'
    end

    return pickup, reason and ("|cff00ff00%s|r"):format(reason)
end)

-- 不让弟弟们用装备
ac.game:on "单位-尝试使用物品" ( function(_, unit, item, player)
    if unit:has_restriction "闭锁" then 
        ac.console:error(player, "该单位无法使用物品")
        return false
    end 
end )

ac.game:on '单位-即将获得物品' (function(_, unit, item)
    local player = unit:get_owner()
    if unit:has_restriction "闭锁" and item:is_class("powerup") then 
        return false
    end 
    local result, reason = unit:dispatch("单位-拾取规则检查", unit, item)
    if result == false then 
        if reason then 
            ac.console:message(player, reason)
        end
        return false
    end 
end)

ac.game:on '单位-发布拾取命令' (function(_, unit, item)
    local player = unit:get_owner()
    if unit:has_restriction "闭锁" and item:is_class("powerup") then 
        return false
    end 
    if unit:dispatch('单位-检查物品合成', unit, item) then
        local point = item:get_point()
        if unit:get_point() * point < 150 then
            unit:notify('单位-合成物品', unit, item)
        end
        return false
    end
    local result, reason = unit:dispatch("单位-拾取规则检查", unit, item)
    if result == false then 
        if reason then 
            ac.console:message(player, reason)
        end
        return false
    end 
end)

ac.game:on "单位-获得物品效果" ( function(_,unit,item)
end )

ac.game:on "单位-失去物品效果" ( function(_,unit,item)
end )

ac.game:on '单位-右键双击物品' (function(_,unit,item)

end)

ac.game:on '物品-被破坏' (function(_,item)
    item:notify 'on_destroy'
end)

ac.game:on '单位-获得神符' (function(_,unit,item)
    item:notify('on_power',unit)
end)

ac.game:on '单位-出售物品' (function(_,unit,item,shop)
    item:notify('on_sold',shop)
end)

ac.game:on "单位-使用物品" (function(_, unit, item, target)
    if not unit:has_restriction "闭锁" then 
        item:notify('on_use', target)
    end 
end)